
import 'package:flutter/material.dart';
import 'package:reabampos/src/reabam/operation/rb_config.dart';
import 'package:reabampos/src/reabam/operation/rb_enum.dart';
import 'package:reabampos/src/reabam/utils/rb_color.dart';
import 'package:reabampos/src/reabam/utils/rb_widget_ss.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

/*
扫描蓝牙设备
 */
class ScanBleDeviceStatePage extends XPageListViewState{

  ScanBleDeviceStatePage();

  @override
  Widget getListItemWidget(listItem, int removeUsePosition) {
    XBaseDevice device=listItem as XBaseDevice;

    return XWidgetUtils.getXBarForLeftRightWidget(
      margin: XWidgetUtils.getEdgeInsets(XTempData.defaultPageMarginLeft, 10, XTempData.defaultPageMarginRight, 0),
      isShowBottomDivider: false,
      leftWidget: XWidgetUtils.getLayoutExpanded(
          XWidgetUtils.getLayoutColumn(
              mainAxisAlignment:MainAxisAlignment.center,
              children: [
                XWidgetUtils.getWidgetText(device.deviceName!,style: XStyleUtils.getTextStyle(
                    fontSize: XTempData.defaultTextFontSize,
                    color: XColorUtils.color333333
                )),
                XWidgetUtils.getWidgetText(device.bluetoothAddress!,style: XStyleUtils.getTextStyle(
                    fontSize: XTempData.defaultSmallTextFontSize,
                    color: XColorUtils.color999999
                ))
              ]
          )
      ),
      rightWidget: XImageUtils.getImageWidgetWithAsset(
          device.isUserSelect?'select_xuanzhong.png':'select_weixuanzhong.png',
        color:device.isUserSelect?RBColors.reabamColor4:null,
        width: RBConfigs.selectImageWidth,
        height: RBConfigs.selectImageWidth,
      ),
      onPressed: (){
        setState(() {
          if(!device.isUserSelect){
            for (var element in widget.listData) {
              XBaseDevice item=element;
              item.isUserSelect=false;
            }
            device.isUserSelect=true;
          }else{
            device.isUserSelect=false;
          }
        });
      },
    );

  }

  @override
  void onStart() {

    if(RBConfigs.versionType==VersionType.officialSelfServiceVersion
        || RBConfigs.versionType==VersionType.testSelfServiceVersion
    ){
      //自助收银
      setXTopBarWidget(
          height: null,
          child: RBWidgets_SelfService.getTitleBarWidget(
            this,
            title: '扫描设备',
            rightBt: '刷新',
            onTapRightBt: (){
              updateData();
            }
          )
      );

    }else{
      //HD h5
      setXTitle(titleText: '扫描设备',
        titleBarHeight: 50,
        isShowLeftWidget: false,
        rightWidgets:[
          XWidgetUtils.getWidgetButtonText(
              text: '刷新',
              buttonStyle: XStyleUtils.getButtonStyle(fontSize: 15,fontColor: RBColors.reabamColor4),
              onPressed: (){
                updateData();
              }
          )
        ],
      );
      //
      setXLeftFixSpace(width:458);
    }

    //
    setXBottomBarWidget(
        child:XWidgetUtils.getXButtonRoundedRectAngle(
          text: '确认',
          fontColor: Colors.white,
          onPressed: () {
            for(XBaseDevice device in widget.listData){
              if(device.isUserSelect){
                //返回选择扫描设备
                XFlutterUtils.finishPage(context,result: device);
                return;
              }
            }
            toast('请选择设备');
          },
          backgroundColor: RBColors.reabamColor4
      ),
      alignment: Alignment.center,
      padding: XWidgetUtils.getEdgeInsets(RBConfigs.bottomBarButtomMarginLeftRight, 0, RBConfigs.bottomBarButtomMarginLeftRight, 0),
      backgroundColor: Colors.white,
    );
  }

  @override
  Future<void> updateData({int pageIndex=1}) async {
    showLoading();
    XBluetoothBLEUtils.scanDevices(
        notifyScanFinish: (List<XBaseDevice> scanResult) {
          hideLoading();
          setXListData_addAllItems(scanResult.toList());
        }
    );
  }

  @override
  void onPaused() {
    super.onPaused();
    hideLoading();
    XBluetoothBLEUtils.stopScan();
  }


}